/*
 * @Description:
 * @Author: chenju
 * @Date: 2021-10-22 14:22:43
 * @LastEditors: qingyang
 * @LastEditTime: 2023-07-26 17:55:00
 */
import { get, post, getBlob } from '@/utils/http';
import type {
  GetSysFileResult,
  getListOrganizationParams,
  GetOrganizationListResult,
  GetCountyResult,
  GetSysFileParams,
  IUnitResult,
  IAuditRemind,
  IAuditListItem,
  SystemDictResult,
  GetListFactoryResult
} from './model/commonModel';
import baseConfig from '@/config';
import { SystemDictDataTypeEnum } from '@/constants/enum';

enum Api {
  // 全部组织列表
  GET_ORGANIZATION_LIST = '/upms/organization/listOrganization',
  // 查询所有子组织列表
  GET_ORGANIZATION_CHILD_LIST = '/upms/organization/listChildByCode',
  // 当前用户组织列表
  GET_CURRENT_ORGANIZATION_LIST = '/upms/organization/listCurrentOrganization',
  // 当前用户组织列表 租户隔离
  GET_CURRENT_ORGANIZATION_LIST_TENANT = '/upms/organization/listUserCurrentEnterpriseOrg',
  // 获取省市区
  GET_COUNTY = '/upms/region/list',
  // 获取系统配置文件
  GET_SYS_FILE = '/file/getSysFile',
  // 因子单位
  GET_FACTOR_UNIT = '/upms/systemDict/getByType',
  // 查询当前组织和父级组织指定角色的用户列表
  GET_AUDITOR_LIST = '/upms/organization/listUserByOrgAndPermission',
  // 填报组织树查询(叶子节点返回指定角色的人员信息)
  GET_ORG_LIST = '/upms/organization/getOrgTreeWithRole',
  /**
   * 下载文件
   */
  GET_DOWNLOAD_FILE = '/file/downloadFile',
  /**
   * 查询整车工厂集合
   */
  GET_LIST_FACTORY = '/upms/organization/listFactory'
}

// 服务器上传文件
export const uploadFilePostUrl = baseConfig.BASE_URL + '/file/uploadFile';
export const newUploadFilePostUrl = baseConfig.BASE_URL + '/file/uploadFile/V2';
export const uploadFilePostUrlV3 = baseConfig.BASE_URL + '/file/uploadFile/V3';

// 全部组织列表
export const getOrganizationList = (): Promise<GetOrganizationListResult> =>
  get(Api.GET_ORGANIZATION_LIST);

// 查询所有子组织列表
export const getChildByCodeList = (): Promise<GetOrganizationListResult> =>
  get(Api.GET_ORGANIZATION_CHILD_LIST);

// 获取当前用户组织列表
export const getCurrentOrganizationList = (
  params?: getListOrganizationParams
): Promise<GetOrganizationListResult> => get(Api.GET_CURRENT_ORGANIZATION_LIST, params);

// 获取当前用户组织列表（租户隔离）
export const getCurrentOrganizationListTenant = (
  params?: getListOrganizationParams
): Promise<GetOrganizationListResult> => get(Api.GET_CURRENT_ORGANIZATION_LIST_TENANT, params);

export const uploadFilePost = (params: any): Promise<any> =>
  post('/inventory/report/batchUpload', params);

// 获取省市区
export const getCounty = (regionCode?: string, hasCounty = true): Promise<GetCountyResult> =>
  get(Api.GET_COUNTY, { regionCode, hasCounty });

// 获取组织树
export const getOrgTree = (): Promise<any> => get('/upms/organization/getOrganizationTree');

// 获取系统配置文件
export const getSysFile = (params: GetSysFileParams): Promise<GetSysFileResult> =>
  get(Api.GET_SYS_FILE, {
    application: 'cmp',
    ...params
  });

// 根据类型查询数据字典(因子单位)
export const getBase = (
  dataType: 'inventory:FACTOR_UNIT' | 'footprint:FACTOR_UNIT'
): Promise<IUnitResult[]> => get(Api.GET_FACTOR_UNIT, { dataType });

// 获取首页审核提醒count
export const getAuditRemind = (): Promise<IAuditRemind> => get('/upms/report/auditRemind');

// 获取审核人列表
export const getAuditorList = (orgCode: string, permission: string): Promise<IAuditListItem[]> =>
  get(Api.GET_AUDITOR_LIST, { permission, orgCode });
// 填报组织树查询
export const getFillOrgTree = (roleCode: 'inventory_manager', orgCode: string): Promise<any[]> =>
  get(Api.GET_ORG_LIST, { roleCode, orgCode });

/**
 * 获取数据字典
 */
export const getSystemDict = (
  dataType: keyof typeof SystemDictDataTypeEnum
): Promise<SystemDictResult[]> => get(Api.GET_FACTOR_UNIT, { dataType });

/**
 * 图片上传服务器生成url
 */
export const uploadImg = (params: any): Promise<any> => post('/file/uploadFile/V3', params);

/**
 * 下载文件
 * @param fileUrl
 */
export const getBlobFileByUrl = (fileUrl: string) => getBlob(Api.GET_DOWNLOAD_FILE, { fileUrl });

/**
 * 查询整车工厂集合
 * @returns
 */
export const getListFactory = (): Promise<GetListFactoryResult[]> => get(Api.GET_LIST_FACTORY);
